package com.zhengshang.web.controller.app;

import java.io.IOException;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.WriterException;
import com.zhengshang.common.oss.OssUtils;
import com.zhengshang.common.utils.DateUtils;
import com.zhengshang.common.utils.QRCodeUtils;
import com.zhengshang.common.utils.SecurityUtils;
import com.zhengshang.common.utils.uuid.IdUtils;
import com.zhengshang.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhengshang.common.annotation.Log;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.enums.BusinessType;
import com.zhengshang.api.domain.AppVersion;
import com.zhengshang.api.service.IAppVersionService;
import com.zhengshang.common.utils.poi.ExcelUtil;
import com.zhengshang.common.core.page.TableDataInfo;

/**
 * APP版本更新Controller
 * 
 * @author zhengshang
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/app/version")
public class AppVersionController extends BaseController
{
    @Autowired
    private IAppVersionService appVersionService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询APP版本更新列表
     */
    @PreAuthorize("@ss.hasPermi('app:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppVersion appVersion)
    {
        startPage();
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        return getDataTable(list);
    }

    /**
     * 导出APP版本更新列表
     */
    @PreAuthorize("@ss.hasPermi('app:version:export')")
    @Log(title = "APP版本更新", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppVersion appVersion)
    {
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        ExcelUtil<AppVersion> util = new ExcelUtil<AppVersion>(AppVersion.class);
        return util.exportExcel(list, "version");
    }

    /**
     * 获取APP版本更新详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(appVersionService.selectAppVersionById(id));
    }

    /**
     * 新增APP版本更新
     */
    @PreAuthorize("@ss.hasPermi('app:version:add')")
    @Log(title = "APP版本更新", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppVersion appVersion)
    {
        appVersion.setCreateBy(SecurityUtils.getUsername());
        try {
            String fileUrl = appVersion.getFilesUrl();
            //安卓的需要拼接域名等
            if ("1".equals(appVersion.getVersionType()) && appVersion.getVersionType().indexOf("https://") == -1) {
                fileUrl = configService.selectConfigByKey("sys.oss.file.header") + fileUrl;
            }
            String qrcodeBase64 = QRCodeUtils.createQRCode(fileUrl);
            String base64 = qrcodeBase64.split("base64,")[1];
            JSONObject object = OssUtils.uploadFile(base64, OssUtils.FILE_TYPE_QRCODE, IdUtils.simpleUUID() + ".png");
            if (object != null) {
                appVersion.setQrCodeUrl(object.getString("objectID"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return toAjax(appVersionService.insertAppVersion(appVersion));
    }

    /**
     * 修改APP版本更新
     */
    @PreAuthorize("@ss.hasPermi('app:version:edit')")
    @Log(title = "APP版本更新", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppVersion appVersion)
    {
        appVersion.setUpdateBy(SecurityUtils.getUsername());
        try {
            String fileUrl = appVersion.getFilesUrl();
            //安卓的需要拼接域名等
            if ("1".equals(appVersion.getVersionType()) && appVersion.getVersionType().indexOf("https://") == -1) {
                fileUrl = configService.selectConfigByKey("sys.oss.file.header") + fileUrl;
            }
            String qrcodeBase64 = QRCodeUtils.createQRCode(fileUrl);
            String base64 = qrcodeBase64.split("base64,")[1];
            JSONObject object = OssUtils.uploadFile(base64, OssUtils.FILE_TYPE_QRCODE, IdUtils.simpleUUID() + ".png");
            if (object != null) {
                appVersion.setQrCodeUrl(object.getString("objectID"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return toAjax(appVersionService.updateAppVersion(appVersion));
    }

    /**
     * 删除APP版本更新
     */
    @PreAuthorize("@ss.hasPermi('app:version:remove')")
    @Log(title = "APP版本更新", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appVersionService.deleteAppVersionByIds(ids));
    }

    /**
     * 更新二维码
     */
    @PreAuthorize("@ss.hasPermi('app:version:edit')")
    @PostMapping("/updateQrcode")
    public AjaxResult updateQrcode(@PathVariable Long id)
    {
        AppVersion appVersion = appVersionService.selectAppVersionById(id);
        try {
            String fileUrl = appVersion.getFilesUrl();
            //安卓的需要拼接域名等
            if ("1".equals(appVersion.getVersionType()) && appVersion.getVersionType().indexOf("https://") == -1) {
                fileUrl = configService.selectConfigByKey("sys.oss.file.header") + fileUrl;
            }
            String qrcodeBase64 = QRCodeUtils.createQRCode(fileUrl);
            String base64 = qrcodeBase64.split("base64,")[1];
            JSONObject object = OssUtils.uploadFile(base64, OssUtils.FILE_TYPE_QRCODE, IdUtils.simpleUUID() + ".png");
            if (object != null) {
                appVersion.setQrCodeUrl(object.getString("objectID"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
        appVersion.setUpdateTime(DateUtils.getNowDate());
        appVersion.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(appVersionService.updateQrcode(appVersion));
    }
}
